package com.css.zfzx.sjcj.modules.yhdisaster.service.impl;

import com.css.zfzx.sjcj.modules.yhdisaster.repository.YhAnalysisQueryRepository;
import com.css.zfzx.sjcj.modules.yhdisaster.repository.YhDisasterRepository;
import com.css.zfzx.sjcj.modules.yhdisaster.repository.entity.YhDisasterEntity;
import com.css.zfzx.sjcj.modules.yhdisaster.service.YhAnalysisQueryService;
import com.css.zfzx.sjcj.modules.yhdisaster.viewobjects.AnalysisQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class YhAnalysisQueryServiceImpl implements YhAnalysisQueryService {
    @Autowired
    YhDisasterRepository yhDisasterRepository;
    @Autowired
    YhAnalysisQueryRepository yhAnalysisQueryRepository;
    @Override
    public List<Map> queryCountYhEarthFortification(AnalysisQueryVo analysisQueryVo) {
        String[] arrys=new String[]{"I类（甲类）" ,"II类（乙类）","III类（丙类）","IV类（丁类）"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhEarthFortification(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<Map> queryCountYhEarthIntensity(AnalysisQueryVo analysisQueryVo) {
        String[] arrys=new String[]{"不设防","6度","7度（0.1g）","7度（0.15g）","8度（0.2g）","8度（0.3g）","9度","其他"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhEarthIntensity(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }

        return mapList;
    }

    @Override
    public List<Map> queryCountYhNowEarthQh(AnalysisQueryVo analysisQueryVo) {
        String[] arrys=new String[]{"6度","7度（0.1g）","7度（0.15g）","8度（0.2g）","8度（0.3g）","9度"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhNowEarthQh(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }

        return mapList;
    }

    @Override
    public List<Map> queryCountYhFieldType(AnalysisQueryVo analysisQueryVo) {
        String[] arrys=new String[]{"Ⅰ类场地","Ⅱ类场地","Ⅲ类场地","Ⅳ类场地"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhFieldType(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }

        return mapList;
    }

    @Override
    public List<Map> queryCountYhLotType(AnalysisQueryVo analysisQueryVo) {
        String[] arrys=new String[]{"有利","一般","不利","危险"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhLotType(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }

        return mapList;
    }

    @Override
    public List<Map> queryCountYhExistingVirus(AnalysisQueryVo analysisQueryVo) {
        //String[] arrys=new String[]{"无","轻微","一般","较大","严重"};
        String[] arrys=new String[]{"轻微","一般","重点"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setYhExistingVirus(arr);
                BigInteger countYhEarthFortification = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", countYhEarthFortification);
                map.put("name", arr);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<Map> queryCountYhEarth(AnalysisQueryVo analysisQueryVo) {
        List<Map> mapList=new ArrayList<>();
        List<YhDisasterEntity> yhFortifications = yhAnalysisQueryRepository.queryFortificationBig(analysisQueryVo);
        //原抗震设防烈度高于现抗震设防烈度
        List<YhDisasterEntity> yhHight=new ArrayList<>();
        //原抗震设防烈度等于现抗震设防烈度
        List<YhDisasterEntity> yhEqual=new ArrayList<>();
        //原抗震设防烈度低于现抗震设防烈度
        List<YhDisasterEntity> yhLow=new ArrayList<>();
        for (YhDisasterEntity entity:yhFortifications) {
            String yhEarthIntensity = entity.getYhEarthIntensity();
            String yhNowEarthQh = entity.getYhNowEarthQh();

            if(yhEarthIntensity.equals("其他")||yhEarthIntensity.equals("不设防")){
                yhLow.add(entity);
            }else{
                Double  intensity= subStr(yhEarthIntensity);
                Double nowQh = subStr(yhNowEarthQh);
                if(intensity>nowQh){
                    yhHight.add(entity);
                }
                if(intensity==nowQh){
                    yhEqual.add(entity);
                }
                if(intensity<nowQh){
                    yhLow.add(entity);
                }
            }
        }
        HashMap mapLow=new HashMap();
        mapLow.put("name","原抗震设防烈度低于现抗震设防烈度");
        mapLow.put("value",yhLow.size());
        HashMap mapEq=new HashMap();
        mapEq.put("name","原抗震设防烈度等于现抗震设防烈度");
        mapEq.put("value",yhEqual.size());
        HashMap mapHigh=new HashMap();
        mapHigh.put("name","原抗震设防烈度高于现抗震设防烈度");
        mapHigh.put("value",yhHight.size());
        mapList.add(mapLow);
        mapList.add(mapEq);
        mapList.add(mapHigh);
        return mapList;
    }
//----------------------------
    /**
     * 获取评估结果
     * @param analysisQueryVo
     * @return
     */
    @Override
    public List<Map> queryCountEvaluationResultEarth(AnalysisQueryVo analysisQueryVo) {
        //String[] arrys=new String[]{"无","一般","较大","重大","特别重大"};
        String[] arrys=new String[]{"轻微","一般","重点"};
        List<Map> mapList=new ArrayList<>();
        for (String arr: arrys){
            if(!StringUtils.isEmpty(analysisQueryVo.getType())){
                analysisQueryVo.setEvaluationResult(arr);
                BigInteger evaluationResult = yhAnalysisQueryRepository.queryCountAnalysis(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", evaluationResult);
                map.put("name", arr);
                mapList.add(map);
            }
        }

        return mapList;
    }
//==================================
    /**
     * 截取数值
     * @param str
     * @return
     */
    public Double subStr(String str){
        if("6度".equals(str)){
            Double integer = Double.valueOf(str.split("度")[0]);
            return integer;
        }
        if("7度（0.1g）".equals(str)){
            Double sub = sub(str);
            return sub;
        }
        if("7度（0.15g）".equals(str)){
            Double sub = sub(str);
            return sub;
        }
        if("8度（0.2g）".equals(str)){
            Double sub = sub(str);
            return sub;
        }
        if("8度（0.3g）".equals(str)){
            Double sub = sub(str);
            return sub;
        }
        if("9度".equals(str)){
            Double integer = Double.valueOf(str.split("度")[0]);
            return integer;
        }
        return null;
    }

    /**
     * 截取 2度（0.2）获得0.2
     * @param str
     * @return
     */
    public Double sub(String str){
        String a=str.split("度")[1].substring(1,4);
        Double b = Double.valueOf(a);
        return b;
    }
}
